app.register.controller("UserAccountCtrl", ["$rootScope", "$scope", "$location", "$timeout", "$window", "ToastMessage", "SecurityService", "StockService", "FundService", "$q", function($rootScope, $scope, $location, $timeout, $window, ToastMessage, SecurityService, StockService, FundService, $q) {
    function initialVariables() {
        $scope.totalAsserts = 0,
        $scope.estimatedStockValue = -1,
        $scope.asserts = {},
        $scope.loading = {}
    }
    function initTotalMoney() {
        $scope.estimatedStockValue = 0;
        var totalAsserts = 0
          , totalYE = 0;
        $scope.CNY_YE && (totalAsserts += $scope.CNY_YE,
        totalYE += $scope.CNY_YE),
        $scope.HKD_RATE && $scope.HKD_YE && (totalAsserts += $scope.HKD_YE * $scope.HKD_RATE,
        totalYE += $scope.HKD_YE * $scope.HKD_RATE),
        $scope.USD_RATE && $scope.USD_YE && (totalAsserts += $scope.USD_YE * $scope.USD_RATE,
        totalYE += $scope.USD_YE * $scope.USD_RATE),
        $scope.CNY_totalMoney && (totalAsserts += $scope.CNY_totalMoney,
        $scope.estimatedStockValue += $scope.CNY_MARKET),
        $scope.HKD_RATE && $scope.HKD_totalMoney && (totalAsserts += $scope.HKD_totalMoney * $scope.HKD_RATE,
        $scope.estimatedStockValue += $scope.HKD_MARKET * $scope.HKD_RATE),
        $scope.USD_RATE && $scope.USD_totalMoney && (totalAsserts += $scope.USD_totalMoney * $scope.USD_RATE,
        $scope.estimatedStockValue += $scope.USD_MARKET * $scope.USD_RATE),
        $scope.totalAsserts = totalAsserts / $scope.USD_RATE,
        $scope.totalYE = totalYE / $scope.USD_RATE
    }
    initialVariables(),
    $scope.defined = function(v, undefinedValue) {
        return "undefined" == typeof $scope[v] ? !1 : !($scope[v] === undefinedValue)
    }
    ,
    $scope.formatPercent = function(percent) {
        return percent = percent || 0,
        percent >= 0 ? "+" + percent + "%" : percent + "%"
    }
    ,
    $scope.hasPosition = function(module) {
        return $scope.asserts[module] && $scope.asserts[module].stock && $scope.asserts[module].stock.marketMoney > 0 ? !0 : !1
    }
    ;
    var funs = {
        isUsT0: function() {
            StockService.t0Status(8003).success(function(data) {
                if (data && data.success) {
                    var status = data.result;
                    $scope.isUsT0 = "SUCCESS" == status
                }
            })
        },
        loadMainExchange: function() {
            FundService.getExchangeRate("USD", "R").success(function(data) {
                data && data.success && ($scope.USD_RATE = data.result,
                initTotalMoney())
            })
        },
        loadStatus: function() {
            var deferred = $q.defer();
            return SecurityService.status(function(data) {
                if (data && data.success) {
                    $scope.status = data.result;
                    var headImg = $rootScope.formatImgUrl($scope.status.credential.headimg);
                    headImg && $(".A_head_img").attr("src", headImg)
                }
                deferred.resolve()
            }),
            deferred.promise
        },
        loadAccountAsserts: function() {
            var queryAccountPromise = FundService.queryAccountSummary();
            return queryAccountPromise.success(function(data) {
                if (data && data.success) {
                    $scope.estimatedValue = 0,
                    angular.forEach(data.result, function(item) {
                        var totalMoney = item.availableMoney;
                        totalMoney *= item.rate,
                        $scope.estimatedValue += totalMoney
                    });
                    for (var i = 0; i < data.result.length; i++)
                        "CNY" == data.result[i].type ? ($scope.CNY_YE = data.result[i].availableMoney,
                        $scope.CNY_RATE = data.result[i].rate) : "HKD" == data.result[i].type ? ($scope.HKD_YE = data.result[i].availableMoney,
                        $scope.HKD_RATE = data.result[i].rate) : "USD" == data.result[i].type && ($scope.USD_YE = data.result[i].availableMoney,
                        $scope.USD_RATE = data.result[i].rate);
                    initTotalMoney()
                }
            }),
            queryAccountPromise
        },
        loadAsserts: function(module) {
            if ($scope.isTradable(module)) {
                var summaryPromise = StockService.summary(module);
                return summaryPromise.success(function(data) {
                    $scope.loading[module] = !0,
                    data && data.success && data.result && ($scope.asserts[module] = data.result,
                    "8001" == module ? ($scope.CNY_totalMoney = data.result.current.totalMoney,
                    $scope.CNY_MARKET = data.result.stock.marketMoney) : "8002" == module ? ($scope.HKD_totalMoney = data.result.current.totalMoney,
                    $scope.HKD_MARKET = data.result.stock.marketMoney) : "8003" == module && ($scope.USD_totalMoney = data.result.current.totalMoney,
                    $scope.USD_MARKET = data.result.stock.marketMoney),
                    initTotalMoney())
                }).error(function() {
                    $scope.loading[module] = !0
                }),
                summaryPromise
            }
        }
    };
    $scope.loadData = function() {
        return initialVariables(),
        $q.all([funs.loadStatus(), funs.loadAccountAsserts(), funs.loadAsserts(8001), funs.loadAsserts(8002), funs.loadAsserts(8003)])
    }
    ,
    $scope.loadData(),
    funs.isUsT0(),
    funs.loadMainExchange()
}
]);
